<%@page import="com.mde.exceptions.WrongPointDataException"%>
<%@ page 
	language="java" 
	contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>    
<%@ page import="java.io.File"%>
<%@ page import="com.mde.util.Validator"%>
<%@ page import="java.util.Scanner"%>
<%@ page import="java.sql.*" %>
<%@ page errorPage="error.jsp" %> 
<!DOCTYPE html>
<html>
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta charset="utf-8"/>
	<title>Adding point to database</title>
</head>
<body>
	<%!
		Statement stmt;
		Connection con;
		String url = Validator.dbURL;
	%>
	<%	
		String lat = request.getParameter("latitude");
		String lon = request.getParameter("longitude");
		String type = request.getParameter("type");
		String name = request.getParameter("name");
		
		if (Validator.validatePointData(lat, lon, type, name)) {
			String insert_query = 
					"INSERT INTO Points (latitude, longitude, type, name) " + 
					"VALUES (" + lat + 
					", " + lon +  
					", \"" + type + "\"" +
					", \"" + name + "\"" +
					");";
				
			String str = new String(insert_query.getBytes("ISO8859_1"), "UTF8");   
				
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(url, "root", "12345"); 
			stmt = con.createStatement();
			try {
				stmt.execute(str);
				out.println("Point successfully added!");
			} catch(Exception e) {
				out.println(e.getMessage());
			}
		} else {
			throw new WrongPointDataException("Wrong data for point");
		}	
	%>
</body>
</html>